Home:ALL Converter>Sharepoint Rest Api calls`

Sharepoint Rest Api calls`

Ask Time:2016-10-18T15:39:28         Author:Sonali

Json Formatter

I am making a simple website where I have to save some of my data to sharepoint list. I am using only html css js only. I need to make REST calls to Sharepoint APIs to post data on SP. I am trying to get data from my list(in SP) as follow:

$(document).ready(function() {
        processListItems(hostweburl, 'ListName');
    });

function processListItems(url, listname) {

        $.ajax({
            url: url + "/_api/web/lists/getbytitle('" + listname + "')",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function(data) {
                console.log(data);      
            },
            error: function(data) {
                console.log(data);
            }
        });
    }

It returns this response:

"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"

I am referencing only two scripts:

 <script src="js/jquery-3.1.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>

It says I am unauthorised to access. Although I am having admin rights to my list. Please tell me how to fix this issue. Do I need any authorization header for ajax call to Rest Api? I am new to Sharepoint. Please Help!

Author:Sonali,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40102310/sharepoint-rest-api-calls
RichG82 :

I think that custom scripting is disabled by default in SPOnline. You may have to enable it. These links may help:\n\nAccess denied office 365 / SharePoint online with Global Admin account\n\nhttp://vamsi-sharepointhandbook.blogspot.com/2015/07/turn-scripting-capabilities-on-or-off.html",
2016-10-18T15:45:11
Pranav Ghosh :

When you are trying to pull items from a list, specify the URL up to items. See below line for syntax -\n\n$.ajax({\nurl: _spPageContextInfo.webAbsoluteUrl + \"/_api/web/lists/getByTitle('\"+listName+\"')/items\",\n// Rest of the code\n});\n",
2018-03-28T11:06:24
yy